#!/usr/bin/env groovy

docker_registry_server = ImageTag.split(':')[0..1].join(':')
currentBuild.displayName = ImageTag.split(':')[2] + "-${arch}"
currentBuild.description = sourceBranch + ": " + commitHash

if (arch == "MULTI") {
    gpu_list = """
            - "A100_PCIE_40GB"
            - "A100_PCIE_80GB"
            - "A100_80GB_PCIE"
            - "A100_PCIE_100GB"
            - "A10"
            - "A30"
            - "A40"
            - "GA100-E4720-HBM2"
            - "GA100-E4720-DVT"
            - "GA104-300-PG142-SKU10-TS3"
            - "GeForce_RTX_4090"
            - "H100_80GB_HBM3"
            - "GH100_P1010_PCIE_CR"
            - "Tesla_V100_PCIE_32GB"
    """
} else {
    gpu_list = """
            - "${arch}"
    """
}

cudaVerTuple = cudaVer.split('\\.')
cudaMajor = cudaVerTuple[0]
Float cudaMinor = cudaVerTuple[1]
if (cudaMajor == "12" && cudaMinor > 2) {
    node_blacklist = """
            - "ipp1-0581.ipp1a1.colossus"
            - "ipp1-0648.ipp1u1.colossus"
            - "ipp1-0901.ipp1u1.colossus"
            - "ipp1-1926.ipp1u1.colossus"
            - "ipp1-1928.ipp1u1.colossus"
            - "a4u8g-0031.ipp1u1.colossus"
            - "ipp1-0022.ipp1u1.colossus"
            - "ipp1-0136.ipp1u1.colossus"
            - "ipp1-0208.ipp1u1.colossus"
            - "ipp1-0216.ipp1u1.colossus"
            - "ipp1-0218.ipp1u1.colossus"
            - "ipp1-0268.ipp1u1.colossus"
            - "ipp1-0295.ipp1u1.colossus"
            - "ipp1-0323.ipp1u1.colossus"
            - "ipp1-0386.ipp1u1.colossus"
            - "ipp1-0453.ipp1u1.colossus"
            - "ipp1-0454.ipp1u1.colossus"
            - "ipp1-0557.ipp1a1.colossus"
            - "ipp1-0558.ipp1u1.colossus"
            - "ipp1-0574.ipp1u1.colossus"
            - "ipp1-0593.ipp1u1.colossus"
            - "ipp1-0624.ipp1u1.colossus"
            - "ipp1-0776.ipp1u1.colossus"
            - "ipp1-0893.ipp1u1.colossus"
            - "ipp1-0895.ipp1u1.colossus"
            - "ipp1-0897.ipp1u1.colossus"
            - "ipp1-0902.ipp1u1.colossus"
            - "ipp1-1163.ipp1a1.colossus"
            - "ipp1-1257.ipp1u1.colossus"
            - "ipp1-1266.ipp1u1.colossus"
            - "ipp1-1267.ipp1u1.colossus"
            - "ipp1-1335.ipp1u1.colossus"
            - "ipp1-1427.ipp1u1.colossus"
            - "ipp1-1555.ipp1u1.colossus"
            - "ipp1-1556.ipp1u1.colossus"
            - "ipp1-1557.ipp1u1.colossus"
            - "ipp1-1558.ipp1u1.colossus"
            - "ipp1-1560.ipp1u1.colossus"
            - "ipp1-1562.ipp1u1.colossus"
            - "ipp1-1564.ipp1a1.colossus"
            - "ipp1-1565.ipp1u1.colossus"
            - "ipp1-1991.ipp1u1.colossus"
            - "ipp1-2248.ipp1u1.colossus"
            - "ipp1-2250.ipp1u1.colossus"
            - "ipp1-2255.ipp1u1.colossus"
            - "ipp1-2993.ipp1u1.colossus"
            - "ipp2-0946.ipp2u1.colossus"
            - "ipp2-0958.ipp2u1.colossus"
            - "ipp2-1688.ipp2u1.colossus"
            - "nvc-t2-045.ipp2u1.colossus"
            - "s1019-0255.ipp1u1.colossus"
            - "s1019-0256.ipp1u1.colossus"
            - "s1019-0257.ipp1u1.colossus"
            - "s1019-0292.ipp1u1.colossus"
            - "s1019-0293.ipp1u1.colossus"
            - "s1019-0293.ipp1u1.colossus"
            - "x11-0615.ipp1a1.colossus"
            - "x11-0619.ipp1a1.colossus"
    """
} else {
    node_blacklist = """
            - "a4u8g-0031.ipp1u1.colossus"
    """
}

gitlabCommitStatus("test-${configName}-${arch}") {

podTemplate(
cloud:'sc-ipp-blossom-prod',
yaml:"""
apiVersion: v1
kind: Pod
spec:
  volumes:
  - name: pvc-mount
    persistentVolumeClaim:
      claimName: 'kaolin-pvc'
  containers:
  - name: docker
    image: ${imageTag}
    command:
    - cat
    resources: 
        requests:
          nvidia.com/gpu: 1
        limits:
          nvidia.com/gpu: 1
    tty: true
    volumeMounts:
      - mountPath: /mnt
        name: pvc-mount
  imagePullSecrets:
  - name: gitlabcred
  nodeSelector:
    kubernetes.io/os: linux
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: "nvidia.com/gpu_type"
            operator: "In"
            values:${gpu_list}
          - key: "kubernetes.io/hostname"
            operator: "NotIn"
            values:${node_blacklist}
          - key: "nvidia.com/driver_version"
            operator: "NotIn"
            values:
            - "545.23"
""") {
  node(POD_LABEL) {
    container("docker") {
      timeout(time: 230, unit: 'MINUTES') {
        stage("Install deps") {
          sh 'pip install -r /kaolin/tools/ci_requirements.txt "jupyter_client<8"'
          sh 'apt update && apt install -y unzip && unzip /kaolin/examples/samples/rendered_clock.zip -d /kaolin/examples/samples/'
        }
        def build_passed = true
        try {
          stage('Disp info') {
            sh 'nvidia-smi'
            sh 'python --version'
            sh 'lscpu'
            sh 'free -h --si'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Pytest") {
            sh '''
            export KAOLIN_TEST_NVDIFFRAST=1
            export KAOLIN_TEST_SHAPENETV1_PATH=/mnt/data/ci_shapenetv1
            export KAOLIN_TEST_SHAPENETV2_PATH=/mnt/data/ci_shapenetv2
            export KAOLIN_TEST_MODELNET_PATH=/mnt/data/ModelNet
            export KAOLIN_TEST_SHREC16_PATH=/mnt/data/ci_shrec16
            export KAOLIN_TEST_GSPLATS_DIR=/mnt/gsplats
            pytest --durations=50 --import-mode=importlib -rs --cov=/kaolin/kaolin \
                --log-disable=PIL.PngImagePlugin \
                --log-disable=PIL.TiffImagePlugin \
                --log-disable=kaolin.rep.surface_mesh \
                /kaolin/tests/python/
            '''
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Dash3D") {
            sh '''
            pytest -s --cov=/kaolin/kaolin /kaolin/tests/integration/
            '''
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          if (arch == "TITAN_RTX") {
            stage("Doc examples") {
              // using wheel you don't have /kaolin/kaolin
              sh '''
              if [ -f "/kaolin/kaolin" ]; then
                  pytest --doctest-modules --ignore=/kaolin/kaolin/experimental /kaolin/kaolin
              fi
              '''
            }
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        // TUTORIALS
        try {
          stage("BBox Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython bbox_tutorial.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Camera and Rasterization Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython camera_and_rasterization.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("DIB-R Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython dibr_tutorial.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Diffuse lighting Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython diffuse_lighting.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("DMTet Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython dmtet_tutorial.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("GLTF Visualizer") {
            sh 'cd /kaolin/examples/tutorial && ipython gltf_viz.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Interactive Visualizer") {
            sh 'cd /kaolin/examples/tutorial && ipython interactive_visualizer.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Physics - Simplicits Easy API") {
            sh 'cd /kaolin/examples/tutorial/physics/ && ipython simplicits_easy_api.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Physics - Simplicits INRIA Splatting") {
            sh 'cd /kaolin/examples/tutorial/physics/ && ipython simplicits_inria_splatting.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Physics - Simulatable 3DGRUT - 3DGRUT") {
            sh '''#!/bin/bash
                  PYTHON_VERSION=`python --version`
                  PYTHON_MINOR=`echo "$PYTHON_VERSION" | cut -d'.' -f2`
                  if [[ "$PYTHON_MINOR" -ge "11" ]]; then
                      export CONDA_NO_PLUGINS=true
                      DEBIAN_FRONTEND=noninteractive add-apt-repository ppa:ubuntu-toolchain-r/test
                      DEBIAN_FRONTEND=noninteractive apt-get install -y gcc-11 g++-11
                      conda config --set solver classic
                      conda install -c conda-forge mesa-libgl-devel-cos7-x86_64 -y
                      cd /tmp && git clone --recursive https://github.com/nv-tlabs/3dgrut
                      cd /tmp/3dgrut && cp /kaolin/tests/python/examples/install_only_3dgrut.sh . && ./install_only_3dgrut.sh base WITH_GCC11
                      cd /kaolin/examples/tutorial/physics/ && ipython simulatable_3dgrut.ipynb
                  fi
            '''
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Spherical Gaussian lighting Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython sg_specular_lighting.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Understanding SPCs Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython understanding_spcs_tutorial.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Working with meshes Tutorial") {
            sh 'cd /kaolin/examples/tutorial && ipython working_with_meshes.ipynb'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }

        // RECIPES
        try {
          stage("SPC from Pointcloud Recipe") {
            sh 'cd /kaolin/examples/recipes/dataload/ && python spc_from_pointcloud.py'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("SPC Basics Recipe") {
            sh 'cd /kaolin/examples/recipes/spc/ && python spc_basics.py'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Occupancy Sampling Recipe") {
            sh 'cd /kaolin/examples/recipes/preprocess/ && python occupancy_sampling.py'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Fast Mesh Sampling Recipe") {
            sh 'cd /kaolin/examples/recipes/preprocess/ && python fast_mesh_sampling.py --shapenet-dir=/mnt/data/ci_shapenetv2/'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("SPC Dual Octree Recipe") {
            sh 'cd /kaolin/examples/recipes/spc/ && python spc_dual_octree.py'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("SPC Trilinear Interpolation Recipe") {
            sh 'cd /kaolin/examples/recipes/spc/ && python spc_trilinear_interp.py'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("SPC Convolution 3D Recipe") {
            sh 'cd /kaolin/examples/recipes/spc/ && python spc_conv3d_example.py'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        try {
          stage("Quaternions Recipe") {
            sh 'cd /kaolin/examples/recipes/math/ && python quaternions.py'
          }
        } catch(e) {
          build_passed = false
          echo e.toString()
        }
        if (build_passed) {
          currentBuild.result = "SUCCESS"
        } else {
          currentBuild.result = "FAILURE"
          error "Build failed. See logs..."
        }
      }
    }
  }
}

}  // gitlabCommitStatus
